﻿namespace CKFinder.Connector.CommandHandlers
{
    using CKFinder.Connector;
    using CKFinder.Settings;
    using System;
    using System.IO;
    using System.Xml;

    internal class InitCommandHandler : XmlCommandHandlerBase
    {
        public const string K_CHARS = "123456789ABCDEFGHJKLMNPQRSTUVWXYZ";

        protected override void BuildXml()
        {
            string[] strArray;
            Config current = Config.Current;
            bool flag = current.CheckAuthentication();
            XmlNode node = XmlUtil.AppendElement(base.ConnectorNode, "ConnectorInfo");
            XmlUtil.SetAttribute(node, "enabled", flag.ToString().ToLower());
            if (!flag)
            {
                ConnectorException.Throw(500);
            }
            string attributeValue = "";
            string str2 = current.LicenseKey.ToUpper();
            if (1 == ("123456789ABCDEFGHJKLMNPQRSTUVWXYZ".IndexOf(str2[0]) % 5))
            {
                attributeValue = current.LicenseName.ToLower();
            }
            XmlUtil.SetAttribute(node, "s", attributeValue);
            XmlUtil.SetAttribute(node, "c", string.Concat(new object[] { str2[11], str2[0], str2[8], str2[12] }).Trim());
            XmlUtil.SetAttribute(node, "thumbsEnabled", current.Thumbnails.Enabled.ToString().ToLower());
            if (current.Thumbnails.Enabled)
            {
                XmlUtil.SetAttribute(node, "thumbsUrl", current.Thumbnails.Url);
                XmlUtil.SetAttribute(node, "thumbsDirectAccess", current.Thumbnails.DirectAccess.ToString().ToLower());
            }
            XmlNode node2 = XmlUtil.AppendElement(base.ConnectorNode, "ResourceTypes");
            if ((base.Request.QueryString["type"] != null) && (base.Request.QueryString["type"].Length > 0))
            {
                strArray = new string[] { base.Request.QueryString["type"] };
            }
            else
            {
                string defaultResourceTypes = Config.Current.DefaultResourceTypes;
                if (defaultResourceTypes.Length == 0)
                {
                    strArray = new string[current.ResourceTypes.Count];
                    for (int j = 0; j < current.ResourceTypes.Count; j++)
                    {
                        strArray[j] = current.ResourceTypes.GetByIndex(j).Name;
                    }
                }
                else
                {
                    strArray = defaultResourceTypes.Split(new char[] { ',' });
                }
            }
            for (int i = 0; i < strArray.Length; i++)
            {
                string resourceType = strArray[i];
                int computedMask = Config.Current.AccessControl.GetComputedMask(resourceType, "/");
                if ((computedMask & 1) == 1)
                {
                    ResourceType resourceTypeConfig = current.GetResourceTypeConfig(resourceType);
                    string targetDirectory = resourceTypeConfig.GetTargetDirectory();
                    bool flag2 = Directory.Exists(targetDirectory) && (Directory.GetDirectories(resourceTypeConfig.GetTargetDirectory()).Length > 0);
                    XmlNode node3 = XmlUtil.AppendElement(node2, "ResourceType");
                    XmlUtil.SetAttribute(node3, "name", resourceType);
                    XmlUtil.SetAttribute(node3, "url", resourceTypeConfig.Url);
                    XmlUtil.SetAttribute(node3, "allowedExtensions", string.Join(",", resourceTypeConfig.AllowedExtensions));
                    XmlUtil.SetAttribute(node3, "deniedExtensions", string.Join(",", resourceTypeConfig.DeniedExtensions));
                    XmlUtil.SetAttribute(node3, "hash", Util.GetMd5Hash(targetDirectory).Substring(0, 0x10));
                    XmlUtil.SetAttribute(node3, "defaultView", "Thumbnails");
                    XmlUtil.SetAttribute(node3, "hasChildren", flag2 ? "true" : "false");
                    XmlUtil.SetAttribute(node3, "acl", computedMask.ToString());
                }
            }
        }

        protected override bool MustCheckRequest()
        {
            return false;
        }

        protected override bool MustIncludeCurrentFolder()
        {
            return false;
        }
    }
}

